#include "Subject.h"
#include <iostream>
#include "IObserver.h"
ConcreteSubject::~ConcreteSubject()
{
	std::cout << "Goodbye, I was the Subject.\n";
}

void ConcreteSubject::Attach(IObserver* observer)
{
	list_observer_.push_back(observer);
}

void ConcreteSubject::Detach(IObserver* observer)
{
	list_observer_.remove(observer);
}

void ConcreteSubject::Notify()
{
	Notify(nullptr);
}

void ConcreteSubject::Notify(BaseMessageInfo* Data)
{
	std::list<IObserver*>::iterator iterator = list_observer_.begin();
	if (bChanged)
	{
		HowManyObserver();
		while (iterator != list_observer_.end()) {
			(*iterator)->Update(this, Data);
			++iterator;
		}
		bChanged = false;
	}
}

void ConcreteSubject::SetChanged()
{
	bChanged = true;
}

void ConcreteSubject::CreateMessage(std::string message /*= "Empty"*/)
{
	SetChanged();
	this->message_ = message;
	Notify();
}

void ConcreteSubject::HowManyObserver()
{
	std::cout << "There are " << list_observer_.size() << " observers in the list.\n";
}

void ConcreteSubject::SomeBusinessLogic()
{
	this->message_ = "change message message";
	Notify();
	std::cout << "I'm about to do some thing important\n";
}

std::string ConcreteSubject::GetMessage()
{
	return message_;
}
